package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyThemeQueryObject;
import cn.wolfcode.wolf2w.redis.service.IRedisService;
import cn.wolfcode.wolf2w.repository.StrategyThemeRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class StrategyThemeServiceImpl implements IStrategyThemeService {

    @Autowired
    private StrategyThemeRepository strategyThemeRepository;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;

    @Override
    public void insert(StrategyTheme strategyTheme) {
        strategyThemeRepository.insert(strategyTheme);
        strategyTheme.setId(null);
    }

    @Override
    public void delete(String id) {
        strategyThemeRepository.deleteById(id);
    }

    @Override
    public void update(StrategyTheme strategyTheme) {
        strategyThemeRepository.save(strategyTheme);
    }

    @Override
    public StrategyTheme selectOne(String id) {
        StrategyTheme strategyTheme = strategyThemeRepository.findById(id).orElse(null);
        //StrategyTheme strategyTheme = byId.get();
        return strategyTheme;
    }

    @Override
    public List<StrategyTheme> selectAll() {
        List<StrategyTheme> all = strategyThemeRepository.findAll();
        return all;
    }

    //@Override
    //public List<StrategyTheme> getDestByRegionId(String rid) {
    //    //1、通过区域id查询区域对象
    //    Region region = regionService.selectOne(rid);
    //    //2、从区域对象上获取目的地id的集合
    //    List<String> refIds = region.getRefIds();
    //    //3、将目的地id的集合转换成目的地名称的集合
    //    List<StrategyTheme> strategyThemes = strategyThemeRepository.findByIdIn(refIds);
    //    return strategyThemes;
    //}

    @Override
    public Page<StrategyTheme> query(StrategyThemeQueryObject q) {
        //1、创建查询对象
        Query query=new Query();
        //2、判断keyword是否为空，若不为空，则进行条件的拼接
        if(StringUtils.hasLength(q.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(q.getKeyword()));//进行拼接高级查询条件时需要使用regex
        }
        //判断父亲id长度是否为空，若为空，拼接成null
        if(!StringUtils.hasLength(q.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(null));
        }else{
            query.addCriteria(Criteria.where("parentId").is(q.getParentId()));
        }
        //3、添加分页语句
        Pageable pageable = PageRequest.of(q.getCurrentPage()-1, q.getPageSize(), Sort.Direction.DESC, "_id");
        Page<StrategyTheme> page = DBHelper.query(q,query, StrategyTheme.class, template, pageable);
        return page;
    }

    @Override
    public void saveOrUpdate(StrategyTheme strategyTheme) {
        if(strategyTheme.getId()!=null){
            this.update(strategyTheme);
        }else{
            this.insert(strategyTheme);
        }
    }

    //@Override
    //public List<StrategyTheme> queryByStrategyTheme(String destId) {
    //    strategyThemeRepository.queryByDestId
    //    return null;
    //}
}
